//
//  AddViewController.swift
//  MyNoteDemo4
//
//  Created by BZF on 2020/8/24.
//  Copyright © 2020 bzf. All rights reserved.
//

import UIKit

class AddViewController: UIViewController,UITextViewDelegate {

    @IBOutlet weak var contentTV: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.contentTV.delegate = self
        self.contentTV.becomeFirstResponder()
    }
    
    @IBAction func saveAction(_ sender: Any) {
        let note = Note()
        note.date = NSDate()
        note.content = contentTV.text
        NoteRespository.instance.insertData(note, handler: { response in
            print(response.value)
            
            NotificationCenter.default.post(name: Notification.Name(rawValue: "reloadListNotification"),object: nil)
            
            self.contentTV.resignFirstResponder()
            self.dismiss(animated: true, completion: nil)
        })
        
    }
    
    @IBAction func cancelAction(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if(text == "\n"){
            self.contentTV.resignFirstResponder()
            return false
        }
        return true
    }

}
